import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Set;

public class RedisDemoSet {
    public static void test01(Jedis jedis) {
        System.out.println("sadd 和 smembers");
        jedis.flushAll();
        jedis.sadd("key", "111", "222", "333");
        Set<String> result = jedis.smembers("key");
        System.out.println("result: " + result);
    }

    public static void test02(Jedis jedis) {
        System.out.println("srem 和 sismember");
        jedis.flushAll();
        jedis.sadd("key", "111", "222", "333");
        boolean result = jedis.sismember("key", "111");
        System.out.println("result: " + result);
        long key = jedis.srem("key", "111", "222");
        System.out.println("key：" + key);
        result = jedis.sismember("key", "222");
        System.out.println("result: " + result);
    }

    public static void test03(Jedis jedis) {
        System.out.println("里面有几个元素 (scard)");
        jedis.flushAll();
        jedis.sadd("key", "111", "222", "333", "444", "555");
        long result = jedis.scard("key");
        System.out.println("result: " + result);
    }

    public static void test04(Jedis jedis) {
        System.out.println("随机删除一个元素 (spop)");
        jedis.flushAll();
        jedis.sadd("key", "111", "222", "333", "444", "555");
        String result = jedis.spop("key");
        System.out.println("result: " + result);
    }

    public static void test05(Jedis jedis) {
        System.out.println("求交集 (sinter)");
        jedis.flushAll();
        jedis.sadd("key", "111", "222", "333");
        jedis.sadd("key2", "111", "222", "444");

        Set<String> result = jedis.sinter("key", "key2");
        System.out.println("result: " + result);
    }

    public static void test06(Jedis jedis) {
        System.out.println("求交集合并到一个新的键里面 (sinterstore)");
        jedis.flushAll();
        jedis.sadd("key", "111", "222", "333");
        jedis.sadd("key2", "111", "222", "444");
        long len = jedis.sinterstore("key3", "key", "key2");
        System.out.println("len：" + len);

        Set<String> result = jedis.smembers("key3");
        System.out.println("result: " + result);
    }


    public static void main(String[] args) {
        JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");

        try (Jedis jedis = jedisPool.getResource()) {
//            test01(jedis);
//            test02(jedis);
//            test03(jedis);
//            test04(jedis);
//            test05(jedis);
            test06(jedis);
        }
    }
}
